package com.ls.knowledge.vo;


import com.ls.knowledge.enums.ResultCode;
import lombok.*;

import java.io.Serializable;

/**
 * @program: day20210907
 * @author: lishun
 * @create: 2021-09-07 22:02
 **/
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Data
public class Result<T> implements Serializable {
    /**
     * 响应码
     */
    private Integer code;
    /**
     * 请求状态
     */
    private Boolean flag;
    /**
     * 响应的消息
     */
    private String message;
    /**
     *  返回的消息
     */
    private T data;


    /**
     * 默认 成功/失败情况
     */
    public Result(Boolean flag) {
        this.flag = flag;
        this.code = flag? ResultCode.SUCCESS.getCode():ResultCode.FAIL.getCode();
        this.message = flag?ResultCode.SUCCESS.getMessage():ResultCode.FAIL.getMessage();
        this.data = null;
    }

    /**
     * 失败携带消息
     * @param flag
     * @param message
     */
    public Result(Boolean flag, String message) {
        this.flag = flag;
        this.code = flag? ResultCode.SUCCESS.getCode():ResultCode.FAIL.getCode();
        this.message = message;
        this.data = null;
    }

    /**
     * 默认 成功/失败情况,携带数据
     */
    public Result(Boolean flag, T data) {
        this.flag = flag;
        this.code = flag?ResultCode.SUCCESS.getCode():ResultCode.FAIL.getCode();
        this.message = flag?ResultCode.SUCCESS.getMessage():ResultCode.FAIL.getMessage();
        this.data = flag?data:null;
    }

    public Result(Boolean flag, ResultCode resultCode){
        this.flag = flag;
        this.code=resultCode.getCode();
        this.message=resultCode.getMessage();
    }

    public Result(Boolean flag, ResultCode resultCode, T data){
        this.flag = flag;
        this.code=resultCode.getCode();
        this.message=resultCode.getMessage();
        this.data=data;
    }


}
